package org.ciallo.simplemusicplayer.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.adapter.SongAdapter;
import org.ciallo.simplemusicplayer.data.model.Song;
import org.ciallo.simplemusicplayer.viewmodel.FavoritesViewModel;
import org.ciallo.simplemusicplayer.viewmodel.SharedViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jukomu
 * @Description: 显示用户收藏歌曲列表的 Activity
 * @Package: org.ciallo.simplemusicplayer.activity
 * @Project: SimpleMusicPlayer
 * @name: FavoritesActivity
 * @Date: 2025/5/30-02:03
 * @Filename: FavoritesActivity
 */
public class FavoritesActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private RecyclerView recyclerViewFavorites;
    private SongAdapter songAdapter;
    private TextView tvEmptyFavorites;
    private FavoritesViewModel favoritesViewModel;
    private SharedViewModel sharedViewModel; // 用于播放控制

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorites); // 将创建此布局

        toolbar = findViewById(R.id.toolbar_favorites_activity); // 确保ID与布局一致
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(getString(R.string.favorites_title)); // 确保 strings.xml 有此资源
        }

        recyclerViewFavorites = findViewById(R.id.recyclerView_favorites_activity);
        tvEmptyFavorites = findViewById(R.id.textView_empty_favorites_activity);

        // 初始化ViewModel
        favoritesViewModel = new ViewModelProvider(this).get(FavoritesViewModel.class);
        sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);

        setupRecyclerView();
        observeViewModel();
    }

    /**
     * 设置 RecyclerView 及其适配器。
     */
    private void setupRecyclerView() {
        songAdapter = new SongAdapter(
                (song, position) -> { // OnSongClickListener
                    // 点击歌曲项播放整个收藏列表中的这首歌
                    List<Song> currentList = songAdapter.getCurrentList();
                    if (currentList != null && !currentList.isEmpty() && sharedViewModel != null) {
                        sharedViewModel.playSongAtIndex(new ArrayList<>(currentList), position);
                        // 可选：跳转到播放界面
                        // Intent intent = new Intent(FavoritesActivity.this, PlayerActivity.class);
                        // startActivity(intent);
                    }
                },
                (song, anchorView) -> { // OnSongOptionsClickListener
                    // 显示歌曲更多操作对话框
                    // SongOptionsDialogFragment.newInstance(song.getId())
                    //        .show(getSupportFragmentManager(), SongOptionsDialogFragment.TAG);
                    Toast.makeText(this, "更多操作 for " + song.getTitle(), Toast.LENGTH_SHORT).show();
                }
        );
        recyclerViewFavorites.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewFavorites.setAdapter(songAdapter);
    }

    /**
     * 观察 ViewModel 中的 LiveData 以更新UI。
     */
    private void observeViewModel() {
        favoritesViewModel.favoriteSongs.observe(this, songs -> {
            if (songs != null && !songs.isEmpty()) {
                songAdapter.submitList(songs);
                tvEmptyFavorites.setVisibility(View.GONE);
                recyclerViewFavorites.setVisibility(View.VISIBLE);
            } else {
                songAdapter.submitList(new ArrayList<>()); // 清空列表
                tvEmptyFavorites.setVisibility(View.VISIBLE);
                recyclerViewFavorites.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        // 处理Toolbar返回按钮
        if (item.getItemId() == android.R.id.home) {
            onBackPressed(); // 或 finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

